package com.hgl.bi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgl.bi.common.constant.ErrorCode;
import com.hgl.bi.common.model.dto.PageDTO;
import com.hgl.bi.common.model.vo.R;
import com.hgl.bi.common.utils.AssertUtil;
import com.hgl.bi.common.utils.ConvertUtil;
import com.hgl.bi.web.entity.PermissionEntity;
import com.hgl.bi.web.model.dto.PmtSaveDto;
import com.hgl.bi.web.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: BI
 * @description: 权限管理控制层
 * @author: hgl
 * @create: 2025-02-15 00:26
 */
@Tag(name = "权限管理模块")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有权限
     *
     * @return 权限列表
     * @author hgl
     */
    @Operation(summary = "查询所有权限", tags = "权限管理模块")
    @GetMapping("/permission/list/all")
    public R<List<PermissionEntity>> getAllPermissions() {
        List<PermissionEntity> permissions = permissionService.list();
        return R.success(permissions);
    }

    /**
     * 分页查询权限列表
     *
     * @param pageDto 分页信息
     * @return 权限分页信息
     * @author hgl
     */
    @Operation(summary = "查询权限列表", tags = "权限管理模块")
    @GetMapping("/permission/list")
    public R<Page<PermissionEntity>> listPermissions(@ParameterObject PageDTO pageDto) {
        // 非空校验
        AssertUtil.notNull(pageDto, ErrorCode.REQUEST_PARAM_ERROR);
        // 分页查询
        Page<PermissionEntity> page = new Page<>(pageDto.getPage(), pageDto.getSize());
        QueryWrapper<PermissionEntity> wrapper = new QueryWrapper<>();
        if (pageDto.getKeyword() != null && !pageDto.getKeyword().isEmpty()) wrapper
                .like("pmt_name", pageDto.getKeyword())
                .or().like("pmt_code", pageDto.getKeyword())
                .or().like("resource", pageDto.getKeyword())
                .or().like("operation", pageDto.getKeyword());
        Page<PermissionEntity> permissionPage = permissionService.page(page, wrapper);
        return R.success(permissionPage);
    }

    /**
     * 获取权限详情
     *
     * @param id 权限ID
     * @return 权限信息
     * @author hgl
     */
    @Operation(summary = "查询权限详情", tags = "权限管理模块")
    @GetMapping("/permission/info/{id}")
    public R<PermissionEntity> getPermissionInfo(@PathVariable Long id) {
        // 非空校验
        AssertUtil.notNull(id, ErrorCode.REQUEST_PARAM_ERROR);
        // 查询权限
        PermissionEntity permission = permissionService.getById(id);
        return R.success(permission);
    }

    /**
     * 更新权限信息
     *
     * @param permission 权限信息
     * @return R<String>
     * @author hgl
     */
    @Operation(summary = "更新权限信息", tags = "权限管理模块")
    @PostMapping("/permission/update")
    public R<String> updatePermissionInfo(@RequestBody PermissionEntity permission) {
        // 非空校验
        AssertUtil.notNull(permission, ErrorCode.REQUEST_PARAM_ERROR);
        // 更新权限
        UpdateWrapper<PermissionEntity> wrapper = new UpdateWrapper<PermissionEntity>()
                .eq("id", permission.getId());
        if (permission.getPmtName() != null) wrapper.set("pmt_name", permission.getPmtName());
        if (permission.getPmtCode() != null) wrapper.set("pmt_code", permission.getPmtCode());
        boolean updated = permissionService.update(wrapper);
        AssertUtil.isTrue(updated, ErrorCode.UPDATE_FALSE);
        return R.ok("更新成功");
    }

    /**
     * 保存权限信息
     *
     * @param permission 权限信息
     * @return R<String>
     * @author hgl
     */
    @Operation(summary = "保存权限信息", tags = "权限管理模块")
    @PutMapping("/permission/save")
    public R<String> savePermissionInfo(@RequestBody PmtSaveDto permission) {
        // 非空校验
        AssertUtil.notNull(permission, ErrorCode.REQUEST_PARAM_ERROR);
        // 保存权限
        PermissionEntity permissionEntity = ConvertUtil.convertObject(permission, PermissionEntity.class, null);
        boolean saved = permissionService.save(permissionEntity);
        AssertUtil.isTrue(saved, ErrorCode.SAVE_FALSE);
        return R.ok("保存成功");
    }

    /**
     * 删除权限信息
     *
     * @param id 权限ID
     * @return R<String>
     * @author hgl
     */
    @Operation(summary = "删除权限信息", tags = "权限管理模块")
    @DeleteMapping("/permission/remove/{id}")
    public R<String> removePermissionInfo(@PathVariable Long id) {
        // 非空校验
        AssertUtil.notNull(id, ErrorCode.REQUEST_PARAM_ERROR);
        // 删除用户
        boolean removed = permissionService.removeById(id);
        AssertUtil.isTrue(removed, ErrorCode.REMOVE_FALSE);
        return R.ok("删除成功");
    }
}
